package gui.components;

import java.awt.*;  
import java.awt.geom.Rectangle2D;

import javax.swing.*;  
   
public class JImageButton extends JButton  
{  
    Image backgroundImage;  
   
    public JImageButton()  
    {  
        super();  
    }  
   
    public JImageButton( Action a )  
    {  
        super( a );  
    }  
   
    public JImageButton( Icon icon )  
    {  
        super( icon );  
    }  
   
    public JImageButton( String text )  
    {  
        super( text );  
    }  
   
    public JImageButton( String text, Icon icon )  
    {  
        super( text, icon );  
    }  
   
    public void setBackgroundImage( Image image )  
    {  
        MediaTracker mt = new MediaTracker( this );  
        mt.addImage( image, 0 );  
        try  
        {  
            mt.waitForAll();  
            backgroundImage = image;  
        }  
        catch( InterruptedException x )  
        {  
            System.err.println(  
                "Specified background image could not be loaded." );  
        }  
    }  
   
    public void paintComponent( Graphics g )  
    {  
        super.paintComponent( g );  
   
        Color saved = g.getColor();  
        g.setColor( getBackground() );  
        g.fillRect( 0, 0, getWidth(), getHeight() );  
        g.setColor( saved );  
   
        if( backgroundImage != null )  
        {  
            int imageX = ( getWidth() - backgroundImage.getWidth( this ) ) / 2;  
            int imageY = ( getHeight() - backgroundImage.getHeight( this ) ) / 2;  
            g.drawImage( backgroundImage, imageX, imageY, this );  
        }  
   
        if( !getText().equals( "" ) )  
        {  
        	FontMetrics fm = g.getFontMetrics();
        	double h = fm.getHeight();
        	Rectangle2D rectw = fm.getStringBounds(super.getText(), g);
        	double w = rectw.getWidth(); 
            g.drawString( super.getText(), (int)(getWidth() / 2 - w/2), (int)(getHeight() / 2 + h/4) );  
        }  
   
        if( getIcon() != null )  
        {  
            Icon icon = getIcon();  
            icon.paintIcon( this, g, 10, 10 );  
        }  
    }  
   
    public Dimension getPreferredSize()  
    {  
        Dimension oldSize = super.getPreferredSize();  
        Dimension newSize = new Dimension();  
        Dimension returnSize = new Dimension();  
   
        if( backgroundImage != null )  
        {  
            newSize.width = backgroundImage.getWidth( this ) + 1;  
            newSize.height = backgroundImage.getHeight( this ) + 1;  
        }  
   
        if( oldSize.height > newSize.height )  
        {  
            returnSize.height = oldSize.height;  
        }  
        else  
        {  
            returnSize.height = newSize.height;  
        }  
   
        if( oldSize.width > newSize.width )  
        {  
            returnSize.width = oldSize.width;  
        }  
        else  
        {  
            returnSize.width = newSize.width;  
        }  
   
        return( returnSize );  
    }  
} 